package ore.example.pokemoninfo.listener;

import de.tr7zw.nbtapi.NBTItem;
import ore.example.pokemoninfo.utils.ConvertPokemon;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;

public class PokemonInventoryListener implements Listener {
    @EventHandler
    public void onRightClick(PlayerInteractEvent event) {
        if (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
        Player player = event.getPlayer();
        ItemStack item = event.getItem();
        if (item == null) return;

        NBTItem nbti = new NBTItem(item);
        if (!nbti.hasKey("pokemon_data")) return;

        boolean locked = nbti.getBoolean("pokemon_locked");
        String ownerUUID = nbti.getString("owner_uuid");
        if (nbti.getBoolean("pokemon_locked") && !player.getUniqueId().toString().equals(ownerUUID)) {
            player.sendMessage("§c这是神兽或3V宝可梦，需要解锁才能使用！");
            return;
        }

        event.setCancelled(true);
        ConvertPokemon.restorePokemonFromItem(player, item);
    }

}
